package com.gls.security.core.configurer.builder;

import com.gls.security.core.configurer.adapter.HttpSecurityConfigurerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * HttpSecurity 默认配置管理器
 *
 * @author lizhiyong
 */
@Component
public class DefaultHttpSecurityConfigurerBuilder implements HttpSecurityConfigurerBuilder {

    @Autowired
    private List<HttpSecurityConfigurerAdapter> httpSecurityConfigurerAdapters;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        for (HttpSecurityConfigurerAdapter httpSecurityConfigurerAdapter : httpSecurityConfigurerAdapters) {
            if (httpSecurityConfigurerAdapter instanceof SecurityConfigurerAdapter) {
                //自定义filter配置
                SecurityConfigurerAdapter securityConfigurerAdapter = (SecurityConfigurerAdapter) httpSecurityConfigurerAdapter;
                http.apply(securityConfigurerAdapter);
            } else {
                // 默认filter配置
                httpSecurityConfigurerAdapter.configure(http);
            }
        }
    }
}
